package com.boot.dog.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.dog.common.exception.DogException;
import com.boot.dog.common.util.ApiResult;
import com.boot.dog.common.util.Result;
import com.boot.dog.entities.ao.LogAo;
import com.boot.dog.entities.pojo.Log;
import com.boot.dog.mapper.LogMapper;
import com.boot.dog.service.LogService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: MaxLv
 * @Date: 2019/12/2
 */
@Service
@AllArgsConstructor
public class LogServiceImpl implements LogService {

    private final LogMapper logMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Log log) {
        logMapper.insert(log);
    }

    @Override
    public ApiResult<IPage<Log>> getLogs(Page page, LogAo ao) {
        page.addOrder(OrderItem.desc("id"));
        IPage<Log> logs = logMapper.selectByPage(page, ao);
        return Result.of(logs);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ApiResult<Void> aopTest() {
        hand();
        return Result.warn("出错啦！！！！！！");
    }

    private void hand(){
        logMapper.delete(Wrappers.<Log>query().lambda().eq(Log::getLogType, 0));
        throw new DogException("abc");
    }
}
